/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package generic.net;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author adam
 */
public class SubInputStreamTest {

    public SubInputStreamTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    /**
     * Test of read method, of class SubInputStream.
     */
    @Test
    public void testRead() throws IOException {
        byte[] test =  {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

        byte b = 'a';
        System.out.println("Creating some piped streams");
        PipedOutputStream os = new PipedOutputStream();
        PipedInputStream is = new PipedInputStream(os);

        SubOutputStream sos = new SubOutputStream(os);
        System.out.println("Writing a byte and closing substream.");
        sos.write(test);
        sos.close();

        SubInputStream sis = new SubInputStream(is);
        System.out.println("checking available");
        byte[] result = new byte[test.length];
        sis.read(result);
        for(int i = 0; i < result.length; i++) {
            Assert.assertEquals(test[i], result[i]);
        }

        System.out.println("Closing all streams.");
        os.close();
        is.close();
    }

    /**
     * Test of available method, of class SubInputStream.
     */
    @Test
    public void testAvailable() throws IOException {
        System.out.println("Creating some piped streams");
        PipedOutputStream os = new PipedOutputStream();
        PipedInputStream is = new PipedInputStream(os);

        SubOutputStream sos = new SubOutputStream(os);
        System.out.println("Writing a byte and closing substream.");
        sos.write(123);
        sos.close();

        SubInputStream sis = new SubInputStream(is);
        System.out.println("checking available");
        Assert.assertEquals(1, sis.available());

        System.out.println("Closing all streams.");
        os.close();
        is.close();

        System.out.println("Creating a new substream.");
        sis = new SubInputStream(is);;

        System.out.println("Checking that its size is zero.");
        Assert.assertEquals(0, sis.available());
    }

}